﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;


    public class Pic
    {
        #region 图片处理相关
        /// <summary>
        /// 图片名2图片MINI名
        /// </summary>
        /// <param name="picname">图片名</param>
        /// <returns>图片加了MINI之后的名</returns>
        public static string picname2picMINIname(string picname)
        {
            if (picname != "")
            {
                int poingN = picname.LastIndexOf('.');
                if (poingN > 0)
                {
                    string picname_ = picname.Substring(0, poingN);
                    string picExe = picname.Substring(poingN);
                    return picname_ + "_MINI" + picExe;
                }
                else return "";
            }
            else return "";
        }
        /// <summary>
        /// 图片名2图片WEB名
        /// </summary>
        /// <param name="picname">图片名</param>
        /// <returns>图片加了WEB之后的名</returns>
        public static string picname2picWEBname(string picname)
        {
            if (picname != "")
            {
                int poingN = picname.LastIndexOf('.');
                if (poingN > 0)
                {
                    string picname_ = picname.Substring(0, poingN);
                    string picExe = picname.Substring(poingN);
                    return picname_ + "_WEB" + picExe;
                }
                else return "";
            }
            else return "";
        }
        /// <summary>
        /// 图片名2图片MIDDLE名
        /// </summary>
        /// <param name="picname">图片名</param>
        /// <returns>图片加了MIDDLE之后的名</returns>
        public static string picname2picMIDDLEname(string picname)
        {
            if (picname != "")
            {
                int poingN = picname.LastIndexOf('.');
                if (poingN > 0)
                {
                    string picname_ = picname.Substring(0, poingN);
                    string picExe = picname.Substring(poingN);
                    return picname_ + "_MIDDLE" + picExe;
                }
                else return "";
            }
            else return "";
        }
        /// <summary>
        /// 图片名2图片LARGE名
        /// </summary>
        /// <param name="picname">图片名</param>
        /// <returns>图片加了LARGE之后的名</returns>
        public static string picname2picLARGEname(string picname)
        {
            if (picname != "")
            {
                int poingN = picname.LastIndexOf('.');
                if (poingN > 0)
                {
                    string picname_ = picname.Substring(0, poingN);
                    string picExe = picname.Substring(poingN);
                    return picname_ + "_LARGE" + picExe;
                }
                else return "";
            }
            else return "";
        }
        /// <summary>
        /// 通过扩展名判断是否图片
        /// </summary>
        /// <param name="Extension_"></param>
        /// <returns></returns>
        public static bool checkISimg(string Extension_)
        {
            Extension_ = Extension_.ToLower();
            if (Extension_ == "jpg" || Extension_ == "jpeg" || Extension_ == "bmp" || Extension_ == "gif" || Extension_ == "png" || Extension_ == "tif")
                return true;
            else return false;
        }
        public static string kuozhanming(string filename)
        {
            return filename.Substring(filename.LastIndexOf('.') + 1);
        }
        public static string pic2p(string f)
        {
            switch (f.ToLower())
            {
                case "gif":
                    return "ico_gif.png";
                case "png":
                    return "ico_png.png";
                case "jpg":
                    return "ico_jpg.png";
                case "jpeg":
                    return "ico_jpg.png";
                default:
                    return "ico_pic.png";
            }
        }
        public static string file2p(string f)
        {
            switch (f.ToLower())
            {
                case "gif":
                    return "ico_gif.png";
                case "png":
                    return "ico_png.png";
                case "jpg":
                    return "ico_jpg.png";
                case "jpeg":
                    return "ico_jpg.png";

                case "psd":
                    return "psd.jpg";
                case "cdr":
                    return "cdr.jpg";
                case "ai":
                    return "ai.jpg";
                case "rar":
                    return "rar.jpg";
                case "zip":
                    return "rar.jpg";
                case "txt":
                    return "txt.jpg";
                default:
                    return "file.png";
            }
        }
        /// <summary> 
        /// 放大缩小图片尺寸 
        /// </summary> 
        /// <param name="picPath"></param> 
        /// <param name="reSizePicPath"></param> 
        /// <param name="iSize"></param> 
        /// <param name="format"></param> 
        public static void PicSized(string picPath, string reSizePicPath, int iSizeWidth, ImageFormat format)
        {
            Bitmap originBmp = new Bitmap(picPath);
            int w = originBmp.Width;
            int h = originBmp.Height;
            if (w > iSizeWidth)
            {
                h = h * iSizeWidth / w;
                w = iSizeWidth;
            }
            Bitmap resizedBmp = new Bitmap(w, h);
            Graphics g = Graphics.FromImage(resizedBmp);
            //设置高质量插值法   
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //消除锯齿 
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.DrawImage(originBmp, new Rectangle(0, 0, w, h), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);
            resizedBmp.Save(reSizePicPath, format);
            g.Dispose();
            resizedBmp.Dispose();
            originBmp.Dispose();
        }
        /// <summary>
        /// 放在DIV里的图片尺寸
        /// </summary>
        /// <param name="x">图片宽</param>
        /// <param name="y">图片高</param>
        /// <param name="x2">DIV宽</param>
        /// <param name="y2">DIV高</param>
        /// <returns></returns>
        public static string picsize_inDiv(int x, int y, int x2, int y2)
        {
            float X = (float)x;
            float Y = (float)y;
            float X2 = (float)x2;
            float Y2 = (float)y2;
            if (X / Y > X2 / Y2) return " height=" + y2 + " width=" + (X * Y2 / Y);
            else return " width=" + x2 + " height=" + (Y * X2 / X);
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="delfile">文件地址</param>
        public static void delFile(string delfile)
        {
            if (delfile != "")
            {
                delfile = System.Web.HttpContext.Current.Request.MapPath(delfile);
                if (!string.IsNullOrEmpty(delfile))
                {

                    if (File.Exists(delfile)) File.Delete(delfile);

                    string delfile_MINI = picname2picMINIname(delfile);
                    if (File.Exists(delfile_MINI)) File.Delete(delfile_MINI);
                    string delfile_WEB = picname2picWEBname(delfile);
                    if (File.Exists(delfile_WEB)) File.Delete(delfile_WEB);
                    string delfile_MIDDLE = picname2picMIDDLEname(delfile);
                    if (File.Exists(delfile_MIDDLE)) File.Delete(delfile_MIDDLE);
                    string delfile_LARGE = picname2picLARGEname(delfile);
                    if (File.Exists(delfile_LARGE)) File.Delete(delfile_LARGE);
                }
            }
        }
        public static void delFile(object delfile)
        {
            if (DataTypePlus.requestString(delfile) != "") delFile(DataTypePlus.requestString(delfile));
        }
        #endregion
    }
